package com.waiting.serializable;

import java.io.Serializable;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/2/14
 **/
public class Person implements Serializable {

    public Person(String fn, String ln, int a)
    {
        this.firstName = fn; this.lastName = ln; this.age = a;
    }

    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public int getAge() { return age; }
    public Person getSpouse() { return spouse; }

    public void setFirstName(String value) { firstName = value; }
    public void setLastName(String value) { lastName = value; }
    public void setAge(int value) { age = value; }
    public void setSpouse(Person value) { spouse = value; }

    //序列化时，会调用该方法，可以在该方法中，对数据进行加解密
    private void writeObject(java.io.ObjectOutputStream stream)
            throws java.io.IOException
    {
        // "Encrypt"/obscure the sensitive data
        age = age << 2;
        stream.defaultWriteObject();
    }

    //反序列化，会调用readObject方法
    private void readObject(java.io.ObjectInputStream stream)
            throws java.io.IOException, ClassNotFoundException
    {
        stream.defaultReadObject();

        // "Decrypt"/de-obscure the sensitive data
        age = age << 2;
    }
    private Object writeReplace()
            throws java.io.ObjectStreamException
    {
        return new PersonProxy(this);
    }

    public String toString()
    {
        return "[Person: firstName=" + firstName +
                " lastName=" + lastName +
                " age=" + age +
                " spouse=" + (spouse!=null ? spouse.getFirstName() : "[null]") +
                "]";
    }

    private String firstName;
    private String lastName;
    private int age;
    private Person spouse;
}
